const path = require('path');
const fs = require('fs');
const dirPath = path.resolve(__dirname, '../../common/models');
fs.readdir(dirPath, (err, files) => {
  if (!err) {
    console.log(files);
    if (files) {
      files.forEach(file => {
        if (file.endsWith('.js')) {
          let fileName = path.basename(file, '.js');
          let ModelName = '';
          if (fileName.includes('-')) {
            let nameArr = fileName.split('-').map(item => {
              return headUpperCase(item);
            });
            ModelName = nameArr.join('');
          } else {
            ModelName = headUpperCase(fileName);
          }
          fs.readFile(path.resolve(dirPath, file), 'utf8', (readErr, data) => {
            if (readErr) {
              console.error(readErr);
              return;
            }
            let content = data.split('\n');
            content.splice(content.length - 2, 0, `  require('../../common/utils/tool.ts').batchDisableRemoteMethod(${ModelName});`);
            fs.writeFile(path.resolve(dirPath, file), content.join('\n'), appErr => {
              if (appErr) {
                console.appError(err);
                return;
              }
            });
            fs.appendFile(path.resolve(dirPath, file));
            require('../../common/utils/tool.ts').batchDisableRemoteMethod(ModelName);
          });
        }
      });
    }
  }
});

const headUpperCase = fileName => {
  return fileName.slice(0, 1).toUpperCase() + fileName.slice(1);
};
